var carouselIndex = 0,
    textArr,
    parentHeight = null,
    childHeight = null,
    stepNum = 0,
    waitCountNum = 0,
    offsetY=50,
    timeSpace=15,
    HZ=myexplorer(),
    waitTimeTimer=null,
    moveTimer=null;
window.onload= function(){
    getRem(100,621);
};
window.onresize= function(){
    getRem(100,621);
    ajax();
};
function ajax(){
    $.ajax({
        type: "get",
        url: "preview/json/testData.json",
        dataType: "json",
        data: {},
        success: function (json) {
            //清空数组
            textArr = json.result;
            //console.log(textArr);
            textMove();
            console.log("info ---->> 历史上的今天数据初始化了...");
        }
    });
}
///样式重置函数
function resetStyle(){
    $('.contentText p').removeAttr('style');
    $('.contentText font').removeAttr('color');
    $('.contentText a').removeAttr('href');
    $('.contentText a').removeAttr('target');
    $('.contentText font').removeAttr('style');
    $('.contentText span').removeAttr('style');
    $('.contentText i').removeAttr('style');
    $('.contentText em').removeAttr('style');
    $('.contentText a').removeAttr('style');
    $('.contentText h1').removeAttr('style');
    $('.contentText h2').removeAttr('style');
    $('.contentText h3').removeAttr('style');
    $('.contentText h4').removeAttr('style');
    $('.contentText h5').removeAttr('style');
    $('.contentText h6').removeAttr('style');
    $('.contentText span').css({fontSize:0.94+"em"});
    $('.contentText i').css({fontSize:0.94+"em"});
    $('.contentText em').css({fontSize:0.94+"em"});
    $('.contentText h1').css({fontSize:0.94+"em",fontWeight:"normal"});
    $('.contentText h2').css({fontSize:0.94+"em",fontWeight:"normal"});
    $('.contentText h3').css({fontSize:0.94+"em",fontWeight:"normal"});
    $('.contentText h4').css({fontSize:0.94+"em",fontWeight:"normal"});
    $('.contentText h5').css({fontSize:0.94+"em",fontWeight:"normal"});
    $('.contentText h6').css({fontSize:0.94+"em",fontWeight:"normal"});
    $('.contentText img').css({float:'left',width:'33%',margin:'0.2rem'});

}
///内容滚动函数
function textMove(){
    if(!!window.moveTimer)	cancelAnimationFrame(window.moveTimer);
    if (carouselIndex >= textArr.length) carouselIndex = 0;
    //将当前内容赋值
    //标题
    $('.contentTitle').html(textArr[carouselIndex].title);
    //时间
    $('.contentTime').html('时间：'+ textArr[carouselIndex].date +'('+ textArr[carouselIndex].lunar +')');
    //正文
    $('.contentText').html('<div>'+ textArr[carouselIndex].content +'</div>');
    //清除样式，并设置新样式
    resetStyle();
    parentHeight = $('.contentText').height();
    childHeight = $('.contentText>div').height();
    if (childHeight > parentHeight) {
        $('.contentText>div').css({
            "top" : offsetY,
            "-webkit-transform" : "translate3d(0,-"+ stepNum +"px,0)",
            "transform" : "translate3d(0,-"+ stepNum +"px,0)"
        });
        stepNum += 0.2;
        if(stepNum >= (childHeight+offsetY)) {
            stepNum = 0;
            carouselIndex++;
        }
        moveTimer = requestAnimationFrame(textMove);
    } else {
        $('.contentText>div').css({top:0});
        $('.contentText>div').css({
            "-webkit-transform" : "translate3d(0,0,0)",
            "transform" : "translate3d(0,0,0)"
        });
        waitTime();
    }
}
///多条切换时间间隔动效函数
function waitTime(){
    if(!!window.waitTimeTimer)	cancelAnimationFrame(window.waitTimeTimer);
    waitCountNum++;
    if(waitCountNum >=(timeSpace*HZ)){//不需要滚动的内容等待10秒钟
        waitCountNum = 0;
        carouselIndex++;
        if(!!window.moveTimer)	cancelAnimationFrame(window.moveTimer);
        textMove();
    }else {
        window.waitTimeTimer = requestAnimationFrame(waitTime);
    }
}

